part of 'updatable.dart';

class UpdatableList<E> extends UpdatableState<List<E>> with ListMixin<E> {
  UpdatableList([List<E> value = const []]) : super(initValue: value);

  @override
  void add(E element) {
    _value.add(element);
    doUpdate();
  }

  @override
  void insert(int index, E element) {
    _value.insert(index, element);
    doUpdate();
  }

  @override
  void addAll(Iterable<E> iterable) {
    _value.addAll(iterable);
    if (iterable.isNotEmpty) doUpdate();
  }

  @override
  void insertAll(int index, Iterable<E> iterable) {
    _value.insertAll(index, iterable);
    if (iterable.isNotEmpty) doUpdate();
  }

  @override
  bool remove(Object? element) {
    bool result = _value.remove(element);
    if (result) doUpdate();
    return result;
  }

  @override
  E removeAt(int index) {
    final removed = _value.removeAt(index);
    doUpdate();
    return removed;
  }

  @override
  void removeWhere(bool Function(E element) test) {
    _value.removeWhere(test);
    doUpdate();
  }

  @override
  void retainWhere(bool Function(E element) test) {
    _value.retainWhere(test);
    doUpdate();
  }

  @override
  E removeLast() {
    final result = _value.removeLast();
    doUpdate();
    return result;
  }

  @override
  void clear() {
    length = 0;
  }

  @override
  void sort([int Function(E a, E b)? compare]) {
    _value.sort(compare);
    doUpdate();
  }

  @override
  Iterator<E> get iterator => _value.iterator;

  @override
  Iterable<E> get reversed => _value.reversed;

  @override
  int get length => _value.length;

  @override
  set length(int newLength) {
    _value.length = newLength;
    doUpdate();
  }

  @override
  UpdatableList<E> operator +(Iterable<E> other) {
    addAll(other);
    return this;
  }

  @override
  E operator [](int index) => _value[index];

  @override
  void operator []=(int index, E value) {
    _value[index] = value;
    doUpdate();
  }

  @override
  Iterable<E> where(bool Function(E) test) => _value.where(test);

  @override
  Iterable<T> whereType<T>() => _value.whereType<T>();
}
